package cn.mall.org.service.impl;

import cn.mall.base.constants.BaseConstants;
import cn.mall.base.utils.MD5Utils;
import cn.mall.org.domain.Manager;
import cn.mall.org.domain.Shop;
import cn.mall.org.dto.ManagerRoleDTO;
import cn.mall.org.mapper.ManagerMapper;
import cn.mall.org.mapper.ShopMapper;
import cn.mall.org.service.IManagerService;
import cn.mall.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author myf
 * @since 2023-12-02
 */
@Service
public class ManagerServiceImpl extends BaseServiceImpl<Manager> implements IManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public List<Long> getRoleByManager(Long managerId) {
        return managerMapper.getRoleByManagerId(managerId);
    }

    @Override
    public void saveRole(ManagerRoleDTO dto) {
        managerMapper.removeRole(dto.getManagerId());
        managerMapper.saveRole(dto.getManagerId(),dto.getRoleId());
    }

    @Override
    @Transactional
    public void update(Manager manager) {
        Manager manager1 = managerMapper.loadById(manager.getId());
        if (!manager1.getPassword().equals(manager.getPassword())){
            // 密码加密 生成随机字符串盐值
            String salt = UUID.randomUUID().toString();
            String md5Password = MD5Utils.encrypByMd5(salt + manager.getPassword() + "auth");
            manager.setPassword(md5Password);
            manager.setSalt(salt);
        }
        managerMapper.update(manager);
    }
}
